package Hot100;

import DMSXL._2023Before.ListNode_.ListNode;

public class getIntersectionNode {
    public static void main(String[] args) {
        int[] listA = {1};
        int[] listB = {1};
        ListNode headA = new ListNode(listA[0]);
        ListNode headB = new ListNode(listB[0]);
        ListNode preA = headA;
        ListNode preB = headB;
        for (int i = 1; i < listA.length; i++) {
            ListNode temp = new ListNode(listA[i]);
            preA.next = temp;
            preA = preA.next;
        }
        for (int i = 1; i < listB.length; i++) {
            ListNode temp = new ListNode(listB[i]);
            preB.next = temp;
            preB = preB.next;
        }

        System.out.println(getIntersectionNode(headA,headB));
    }
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB==null)
            return null;
        System.out.println("   123");
        ListNode A = headA;
        ListNode B = headB;
        int m = 0,n = 0;
        while(A != null){
            A = A.next;
            m++;
        }
        while(B != null){
            B = B.next;
            n++;
        }
        System.out.println(m);
        System.out.println(n);
        A = headA;
        B = headB;

        if(m > n){
            for (int i = 0; i < m - n; i++) {
                A = A.next;
            }
        } else if (m < n) {
            for (int i = 0; i < n- m; i++) {
                B = B.next;
            }
        }

        while(A != null && B != null)
        {
            if(A == B)
                return A;
            A = A.next;
            B = B.next;
        }
        return null;
    }
}
